package Leetcode;

public class productExceptSelf {
    public int[] productExceptSelf(int[] nums) {
        int[] lpre = new int[nums.length];
        int[] rpre = new int[nums.length];
        int lsum = 1, rsum = 1;
        for (int i = 0; i < nums.length; i++) {
            lsum *= nums[i];
            lpre[i] = lsum;
            rsum *= nums[nums.length - i - 1];
            rpre[nums.length - i - 1] = rsum;
        }
        int[] answer = new int[nums.length];
        answer[0] = rpre[1];
        answer[answer.length - 1] = lpre[lpre.length - 2];
        for (int i = 1; i < answer.length - 1; i++) {
            answer[i] = lpre[i - 1] * rpre[i + 1];
        }
        return answer;
    }
}
